package news.web;

import news.model.CommonResult;
import news.model.Link;
import news.model.User;
import news.service.LinksService;
import news.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/admin/links")
public class LinksController {

    @Autowired
    LinksService linksService;
    @PostMapping(path = "/links")
    public CommonResult links(@RequestBody Link link){
        List<Link> links = linksService.linkList(link);
        CommonResult commonResult = new CommonResult(200,links,"查询成功");
        return commonResult;
    }
    @PostMapping(path = "/changeStatus")
    public CommonResult changeStatus(Integer id,Integer status){
        linksService.changeStatus(id,status);
        CommonResult commonResult = new CommonResult(200,"修改成功");
        return commonResult;
    }
    @GetMapping (path = "/delete")
    public CommonResult delete(Integer id){
        linksService.delete(id);
        CommonResult commonResult = new CommonResult(200,"删除成功");
        return commonResult;
    }
    @PostMapping(path = "/save")
    public CommonResult save(@RequestBody Link link,@RequestHeader String adminToken){
        if(link.getId() == null){
            linksService.save(link,adminToken);
        }else {
            linksService.update(link,adminToken);
        }
        CommonResult commonResult = new CommonResult(200,"保存成功");
        return commonResult;
    }
    @GetMapping (path = "/findLinkById")
    public CommonResult findLinkById(Integer id){
        Link link = linksService.findLinkById(id);
        CommonResult commonResult = new CommonResult(200,link,"查询成功");
        return commonResult;
    }
}
